# main.py
from data_loader import load_data
from genetic_algorithm import GeneticAlgorithm
import matplotlib.pyplot as plt


def main():
    instance = load_data()
    ga = GeneticAlgorithm(instance, max_generations=100)
    best_solution, history = ga.solve()

    print("\n最优配送方案：")
    total_cost = 0
    for i, route in enumerate(best_solution):
        route_str = '->'.join(map(str, route))
        distance = sum(instance.distance_matrix[i][j] for i, j in zip(route, route[1:]))
        cost = ga.calculate_fitness([route])
        total_cost += cost
        print(f"路径{i + 1}: {route_str}")
        print(f"  里程: {distance:.1f}km 成本: {cost:.2f}元")

    print(f"\n总成本: {total_cost:.2f}元")

    plt.plot(history)
    plt.title("优化过程收敛曲线")
    plt.xlabel("迭代次数")
    plt.ylabel("总成本（元）")
    plt.grid(True)
    plt.show()

if __name__ == "__main__":
    main()
